<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/28
 * Time: 10:56
 */

namespace app\admin\controller;
use think\Db;
class Config extends Base{
    //
    public function poster(){
        if(request()->isPost()){
            $param = input('post.');
            Db::name('config')->where(array('inc_type'=>'poster'))->setField('value', $param['value']);
            return json(msg(1,url('config/poster'),'编辑成功'));
        }
        $data=Db::name('config')->where(array('inc_type'=>'poster'))->value('value');
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }
    public function crediturl(){
        if(request()->isPost()){
            $param = input('post.');
            Db::name('config')->where(array('inc_type'=>'credit_url'))->setField('value', $param['value']);
            return json(msg(1,url('config/crediturl'),'编辑成功'));
        }
        $data=Db::name('config')->where(array('inc_type'=>'credit_url'))->value('value');
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }
    public function tel(){
        if(request()->isPost()){
            $param = input('post.');
            Db::name('config')->where(array('inc_type'=>'tel'))->setField('value', $param['value']);
            return json(msg(1,url('config/tel'),'编辑成功'));
        }
        $data=Db::name('config')->where(array('inc_type'=>'tel'))->value('value');
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }
    public function teamincomedesc(){
        if(request()->isPost()){
            $param = input('post.');
            Db::name('config')->where(array('inc_type'=>'team_income_desc'))->setField('value', $param['value']);
            return json(msg(1,url('config/team_income_desc'),'编辑成功'));
        }
        $data=Db::name('config')->where(array('inc_type'=>'team_income_desc'))->value('value');
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }

    public function vip(){
        if(request()->isAjax()){
            $param = input('param.');
            $selectResult = Db::name('user_level')->order('level ASC,id ASC')->select();
            foreach($selectResult as $key=>$vo){
                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }
            $return['total'] = Db::name('user_level')->count();  // 总数据
            $return['rows'] = $selectResult;
            return json($return);
        }
        return $this->fetch();
    }
    private function makeButton($id)
    {
        return [
            '编辑' => [
                'auth' => 'config/vipedit',
                'href' => url('config/vipedit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ]
        ];
    }
    public function vipEdit(){
        if(request()->isPost()){
            $param = input('post.');
            $ret=Db::name('user_level')->where(array('id'=>input('param.id')))->update($param);
            if($ret===false) return json(msg(-1,'','编辑失败'));
            return json(msg(1,url('config/vip'),'编辑成功'));
        }
        $data=Db::name('user_level')->where(array('id'=>input('param.id')))->find();
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }
    public function company(){
        if(request()->isPost()){
            $param = input('post.');
            $ret=Db::name('articles')->update($param);
            if($ret===false) return json(msg(-1,'','编辑失败'));
            return json(msg(1,url('config/company'),'编辑成功'));
        }
        $data=Db::name('articles')->find();
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }

    //关于我们
    public function info(){
        if(request()->isPost()){
            $param = input('post.');
            if(paramValid($param,'logo,addr,tel,ser,wx')){
                $map=array(
                    'logo'=>$param['logo'],
                    //'appname'=>$param['appname'],
                    //'comp'=>$param['comp'],
                    'addr'=>$param['addr'],
                    'tel'=>$param['tel'],
                    'ser'=>$param['ser'],
                    'wx'=>$param['wx']
                );
                Db::name('config')->where(array('inc_type'=>'info'))->setField('value', json_encode($map));
            }
            return json(msg(1,url('config/info'),'编辑成功'));
        }
        $data=json_decode(Db::name('config')->where(array('inc_type'=>'info'))->value('value'),true);
        $this->assign([
            'data' => $data
        ]);
        return $this->fetch();
    }
}